package archiving.gui;

import gui.util.CheckboxTreeCellRenderer;
import gui.util.ObserverThreestateCheckBox;
import gui.util.ThreestateCheckBox;
import gui.util.event.CheckboxMouseListener;

import java.util.Collection;
import java.util.HashSet;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;



public class JTreeWithObservingCheckboxes {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame("JTree with added observing checkboxes");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Root Node
		ObserverThreestateCheckBox root = 
			new ObserverThreestateCheckBox("Mistress");
		
		// Level 1 children (of root)
		Collection<JCheckBox> slaves0 = new HashSet<JCheckBox>();
		
		ObserverThreestateCheckBox slave01 = 
			new ObserverThreestateCheckBox("Slave1");
		slaves0.add(slave01);
		ObserverThreestateCheckBox slave02 = 
			new ObserverThreestateCheckBox("Slave2");
		slaves0.add(slave02);
		ObserverThreestateCheckBox slave03 = 
			new ObserverThreestateCheckBox("Slave3");
		slaves0.add(slave03);
		
		root.setSlaves(slaves0);
		
		// Level 2 children
		
		// slave1's children
		Collection<JCheckBox> slaves01 = new HashSet<JCheckBox>();
		
		ThreestateCheckBox slave011 = new ThreestateCheckBox("Slave11");
		slaves01.add(slave011);
		ThreestateCheckBox slave012 = new ThreestateCheckBox("Slave12");
		slaves01.add(slave012);
		ThreestateCheckBox slave013 = new ThreestateCheckBox("Slave13");
		slaves01.add(slave013);
		
		slave01.setSlaves(slaves01);
		
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(root);
		
		DefaultMutableTreeNode node01 = new DefaultMutableTreeNode(slave01);
		
		node01.add(new DefaultMutableTreeNode(slave011));
		node01.add(new DefaultMutableTreeNode(slave012));
		node01.add(new DefaultMutableTreeNode(slave013));
		
		rootNode.add(node01);
		rootNode.add(new DefaultMutableTreeNode(slave02));
		rootNode.add(new DefaultMutableTreeNode(slave03));
		
		JTree tree = new JTree(rootNode);
		tree.setCellRenderer(new CheckboxTreeCellRenderer());
		tree.addMouseListener(new CheckboxMouseListener());
		
		frame.add(tree);
		frame.pack();
		frame.setVisible(true);
	}

}
